package com.example.test;

import java.util.ArrayList;

import com.example.test.R.layout;

import domain.ComentarioClass;
import layout.Comentario;
import layout.elementoPublicacion;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class PublicacionActivity extends ActionBarActivity {
	
	ArrayList<Comentario> comentariosArrayLayout;
	ListView comentariosListView;
	TextView titleTextView;
	Button publicarBtn;
	EditText comentarioEditText;
	Bundle bundle;
	
	
	class AdapterComentarios extends ArrayAdapter<Comentario>{
		Activity context;
		
		public AdapterComentarios(Activity context){
			super(context, R.layout.elemento_comentarios, comentariosArrayLayout);
			this.context = context;
		}
		
		public View getView(int posicion, View view, ViewGroup parent){
			LayoutInflater inflater = context.getLayoutInflater();
			View item = inflater.inflate(R.layout.elemento_comentarios,null);
			
			TextView comentarioTV = (TextView) item.findViewById(R.id.tituloPublicacionTextView);
			comentarioTV.setText(comentariosArrayLayout.get(posicion).getTexto());
			
			TextView usernameTV = (TextView) item.findViewById(R.id.usernameTextView);
			usernameTV.setText(comentariosArrayLayout.get(posicion).getUsername());
			
			return item;
		}
		
		
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_publicacion);
		
		
		comentariosListView = (ListView) findViewById(R.id.comentariosListView);
		comentariosArrayLayout = new ArrayList<Comentario>();
		bundle = getIntent().getExtras();
		
		publicarBtn = (Button) findViewById(R.id.publicarBoton);
		comentarioEditText = (EditText) findViewById(R.id.comentarioEditText);
		publicarBtn = (Button) findViewById(R.id.comentarButton);
		
		publicarBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				controller.Controlador.comentarPublicacion(
						comentarioEditText.getText().toString(), bundle.getString("tv_token"), domain.Sesion.username);
				finish();
				startActivity(getIntent());
			}
		});
		
		
		ArrayList<ComentarioClass> comentariosArray = controller.Controlador.getComentarios(bundle.getString("tv_token"));
		titleTextView = (TextView) findViewById(R.id.tituloPublicacionTextView);
		titleTextView.setText(bundle.getString("titulo"));

		if (comentariosArray == null) {
			comentariosArrayLayout.add(new Comentario("NO hay comentarios, se el primero en comentar!",""));
		} else {

			for (int i = 0; i < comentariosArray.size(); i++) {
				comentariosArrayLayout.add(new Comentario(comentariosArray.get(
						i).getTexto(), comentariosArray.get(i).getUsername()));

				Log.i("COMMENT_TEXT", comentariosArray.get(i).getTexto());
				Log.i("COMMENT_USERNAME", comentariosArray.get(i).getUsername());

			}
		}
		
		AdapterComentarios adapter = new AdapterComentarios(this);
		comentariosListView.setAdapter(adapter);
		
		
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.publicacion, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.enviarMensajeItem) {
			Intent enviarmsjAct = new Intent(PublicacionActivity.this,EnviarmsjActivity.class);
			enviarmsjAct.putExtra("tv_token", bundle.getString("tv_token"));
    		startActivity(enviarmsjAct);
			
			
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
